﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using ERPTOOLS.Class;

namespace ERPTOOLS
{
    public partial class Main_Run : Form
    {
        Transfer _tf;
        Thread t1;
        Thread t2;
        Thread t3;
        Thread t4;
        Thread t5;
        Thread t6;
        Thread t7;
        public delegate void ClientDelegate();
        public Main_Run(Transfer tf)
        {
            _tf = tf;
            InitializeComponent();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            _tf.Visible = true;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Cyc cy = new Cyc(checkBox2,"Item");
            cy.Show();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Cyc cy = new Cyc(checkBox3,"PO");
            cy.Show();
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Cyc cy = new Cyc(checkBox4,"PR");
            cy.Show();
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Cyc cy = new Cyc(checkBox5,"Inv");
            cy.Show();
        }

        private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Cyc cy = new Cyc(checkBox6,"ASN");
            cy.Show();
        }

        private void linkLabel6_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Cyc cy = new Cyc(checkBox7,"ERP_ASN");
            cy.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            if (checkBox1.Checked)
            {
                timer1_Tick(sender, e);
                timer1.Enabled = true;
                timer1.Interval = CalcTime("12","00");
            }
            if (checkBox2.Checked)
            {
                if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//ItemCYC") == "按天")
                {
                    timer2_Tick(sender, e);
                    timer2.Enabled = true;
                    timer2.Interval = CalcTime(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ItemExecute").Substring(0, 2), XMLHelper.GetInnerTextByXpath("Configure.xml", "//ItemExecute").Substring(3, 2));
                }
                else
                {
                    timer2_Tick(sender, e);
                    timer2.Enabled = true;
                    timer2.Interval = CalcTime1(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ItemInterval"));
                }
            }
            if (checkBox3.Checked)
            {
                if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//POCYC") == "按天")
                {
                    timer3_Tick(sender, e);
                    timer3.Enabled = true;
                    timer3.Interval = CalcTime(XMLHelper.GetInnerTextByXpath("Configure.xml", "//POExecute").Substring(0, 2), XMLHelper.GetInnerTextByXpath("Configure.xml", "//POExecute").Substring(3, 2));
                }
                else
                {
                    timer3_Tick(sender, e);
                    timer3.Enabled = true;
                    timer3.Interval = CalcTime1(XMLHelper.GetInnerTextByXpath("Configure.xml", "//POInterval"));
                }
            }
            if (checkBox4.Checked)
            {
                if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//PRCYC") == "按天")
                {
                    timer4_Tick(sender, e);
                    timer4.Enabled = true;
                    timer4.Interval = CalcTime(XMLHelper.GetInnerTextByXpath("Configure.xml", "//PRExecute").Substring(0, 2), XMLHelper.GetInnerTextByXpath("Configure.xml", "//PRExecute").Substring(3, 2));
                }
                else
                {
                    timer4_Tick(sender, e);
                    timer4.Enabled = true;
                    timer4.Interval = CalcTime1(XMLHelper.GetInnerTextByXpath("Configure.xml", "//PRInterval"));
                }
            }
            if (checkBox5.Checked)
            {
                if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//InvCYC") == "按天")
                {
                    timer5_Tick(sender, e);
                    timer5.Enabled = true;
                    timer5.Interval = CalcTime(XMLHelper.GetInnerTextByXpath("Configure.xml", "//InvExecute").Substring(0, 2), XMLHelper.GetInnerTextByXpath("Configure.xml", "//InvExecute").Substring(3, 2));
                }
                else
                {
                    timer5_Tick(sender, e);
                    timer5.Enabled = true;
                    timer5.Interval = CalcTime1(XMLHelper.GetInnerTextByXpath("Configure.xml", "//InvInterval"));
                }
            }
            if (checkBox6.Checked)
            {
                if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//ASNCYC") == "按天")
                {
                    timer6_Tick(sender, e);
                    timer6.Enabled = true;
                    timer6.Interval = CalcTime(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ASNExecute").Substring(0, 2), XMLHelper.GetInnerTextByXpath("Configure.xml", "//ASNExecute").Substring(3, 2));
                }
                else
                {
                    timer6_Tick(sender, e);
                    timer6.Enabled = true;
                    timer6.Interval = CalcTime1(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ASNInterval"));
                }
            }
            if (checkBox7.Checked)
            {
                if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERP_ASNCYC") == "按天")
                {
                    timer7_Tick(sender, e);
                    timer7.Enabled = true;
                    timer7.Interval = CalcTime(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERP_ASNExecute").Substring(0, 2), XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERP_ASNExecute").Substring(3, 2));
                }
                else
                {
                    timer7_Tick(sender, e);
                    timer7.Enabled = true;
                    timer7.Interval = CalcTime1(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERP_ASNInterval"));
                }
            }       
            button3.Enabled = true;
        }

        private void Do_action1(RichTextBox rtb)
        {
            if (DoCheck.CheckGYS(XMLHelper.GetInnerTextByXpath("Configure.xml", "//PurSql"), "SELECT RTRIM(SYSMA.MA002) AS GYSQC,ME006 AS GYSBH,ME002 AS QYBH FROM SCMME LEFT JOIN SYSMA ON SCMME.ME002=SYSMA.MA001 WHERE ME001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' and ME005='1'").Rows.Count > 0 ||
                     (Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//Chkcustomer")) && DoCheck.CheckKH(XMLHelper.GetInnerTextByXpath("Configure.xml", "//CutSql"), "SELECT RTRIM(SYSMA.MA003) AS KHQC,MG006 AS KHBH,MG002 AS QYBH FROM SCMMG LEFT JOIN SYSMA ON SCMMG.MG002=SYSMA.MA001 WHERE MG001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' and MG005='1'").Rows.Count > 0)
                     )
            {
                MailHelper.MailSend("smtp.21cn.com", "pupei", "PuZiXi&071114", "pupei@21cn.com", XMLHelper.GetInnerTextByXpath("Configure.xml", "//RecUser"), XMLHelper.GetInnerTextByXpath("Configure.xml", "//Recemail"), "OpenSCM供应商与客户异常检测结果", GetHtmlString("abc", false, DoCheck.CheckGYS(XMLHelper.GetInnerTextByXpath("Configure.xml", "//PurSql"), "SELECT RTRIM(SYSMA.MA002) AS GYSQC,ME006 AS GYSBH,ME002 AS QYBH FROM SCMME LEFT JOIN SYSMA ON SCMME.ME002=SYSMA.MA001 WHERE ME001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' and ME005='1'"), DoCheck.CheckKH(XMLHelper.GetInnerTextByXpath("Configure.xml", "//CutSql"), "SELECT RTRIM(SYSMA.MA003) AS KHQC,MG006 AS KHBH,MG002 AS QYBH FROM SCMMG LEFT JOIN SYSMA ON SCMMG.MG002=SYSMA.MA001 WHERE MG001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' and MG005='1'")));
            }

        }

        private void Do_action2(RichTextBox rtb)
        {
            //for (int i = 10; i > 1; i--)
            //{
            //    this.BeginInvoke(new ClientDelegate(delegate
            //    {
            //        rtb.AppendText(i.ToString());
            //    }));
            //    System.Threading.Thread.Sleep(100);
            //}
            if (PanduanDate(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ItemStrDate"), XMLHelper.GetInnerTextByXpath("Configure.xml", "//ItemEndDate"), timer2))
            {
                string sqlERP = XMLHelper.GetInnerTextByXpath("Configure.xml", "//ItemLift");
                //string sqlSCM = "select MA002 FROM SCMMA where MA001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "'";
                DataTable dt = DBLink.LinkERP(sqlERP, "ERPITEM");
                //DataTable dt1 = DBLink.LinkSCM(sqlSCM, "SCMITEM");
                foreach (DataRow row in dt.Rows)
                {
                    string sqlSCM = "select MA001 FROM SCMMA where MA002='" + row["MA002"].ToString().Replace("'", "''") + "'AND  MA001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "'";
                    if (DBLink.LKSCM(sqlSCM) > 0)
                    {
                        if (row["ZT"].ToString() == "1")
                        {
                            //Update
                            string upSCMsql = "UPDATE SCMMA SET ModiDate='" + row["ModiDate"] + "',ModiBy='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//User") + "',Flag=Flag+1,MA003='" + row["MA003"] + "',MA004='" + row["MA004"].ToString().Replace("'","''") + "',MA005='" + row["MA005"] + "',MA006='" + row["MA006"] + "',MA007='" + row["MA007"] + "',MA008='" + row["MA008"] + "' WHERE MA001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' AND MA002='" + row["MA002"].ToString().Replace("'", "''") + "'";
                            DBLink.LKSCM(upSCMsql);
                        }
                        else
                        {
                            //delete
                        }
                    }
                    //foreach (DataRow row1 in dt1.Rows)
                    //{
                    //    if (row["MA002"].Equals(row1["MA002"]))
                    //    {
                    //        find = true;
                    //        //update
                    //        string upSCMsql = "UPDATE SCMMA SET MA003='" + row["MA003"] + "',MA004='" + row["MA004"] + "',MA005='" + row["MA005"] + "',MA006='" + row["MA006"] + "',MA007='" + row["MA007"] + "',MA008='" + row["MA008"] + "' WHERE MA001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' AND MA002='" + row["MA002"] + "'";
                    //        DBLink.LKSCM(upSCMsql);
                    //    }
                    //}
                    else
                    {
                        if (row["ZT"].ToString() == "1")
                        {
                            //insert
                            string upSCMsql = "INSERT SCMMA(CreateDate,CreateBy,ModiDate,ModiBy,Flag,MA001,MA002,MA003,MA004,MA005,MA006,MA007,MA008,MA009,MA010,MA011,MA012) VALUES (SYSDATETIME(),'" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//User")+"','"+row["ModiDate"] +"','',1,'" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "','" + row["MA002"].ToString().Replace("'", "''") + "','" + row["MA003"] + "','" + row["MA004"].ToString().Replace("'", "''") + "','" + row["MA005"] + "','" + row["MA006"] + "','" + row["MA007"] + "','" + row["MA008"] + "','" + row["MA009"] + "','" + row["MA010"] + "','" + row["MA011"] + "','" + row["MA012"] + "')";
                            DBLink.LKSCM(upSCMsql);
                        }
                        else
                        {
                            //什么也不做
                        }

                    }
                    //将对应的“更新数据表”中的“更新码字段”置为3。
                    string upERPsql = "UPDATE " + XMLHelper.GetInnerTextByXpath("Configure.xml", "//ItemUpdateDB") + " SET " + XMLHelper.GetInnerTextByXpath("Configure.xml", "//ItemUpdateField") + "='3' WHERE MB001='" + row["MA002"] + "'";
                    DBLink.LKERP(upERPsql);
                }
            }
            else
            {

            }
            if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//ItemCYC") == "按天")
            {
                timer2.Interval = 24 * 60 * 60 * 1000;
            }
        }
        private void Do_action3(RichTextBox rtb)
        {
            if (PanduanDate(XMLHelper.GetInnerTextByXpath("Configure.xml", "//POStrDate"),XMLHelper.GetInnerTextByXpath("Configure.xml", "//POEndDate"),timer3))
            {
                // 1）执行查询SQL。
                string sqlERP = XMLHelper.GetInnerTextByXpath("Configure.xml", "//POLift");
                DataTable dt = DBLink.LinkERP(sqlERP, "ERPPO");
                if (dt.Rows.Count > 0)
                {
                    List<string> SUP = new List<string>(); 
                    dt = DBLink.TableaddSupCol(dt);
                    foreach (DataRow row in dt.Rows)
                    {
                        if (!SUP.Contains(row["SUP"]))
                        {
                            SUP.Add(row["SUP"].ToString());
                        }
                        if (row["ZT"].ToString() == "2")
                        {
                            //delete
                            string delSCMsql = "DELETE FROM SCMTB WHERE TB001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' AND TB003='" + row["TB003"] + "'";
                            DBLink.LKSCM(delSCMsql);
                        }
                        else
                        {
                            string sqlSCM = "select TB003 FROM SCMTB where TB001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "'AND  TB003='" + row["TB003"] + "'";
                            if (DBLink.LKSCM(sqlSCM) > 0)
                            {
                                string upSCMsql = "UPDATE SCMTB SET ModiDate='" + row["ModiDate"] + "',ModiBy='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//User") + "',Flag=Flag+1,TB004='" + row["TB004"] + "',TB005='" + row["TB005"] + "',TB006='" + row["TB006"] + "',TB007='" + row["TB007"] + "',TB008='" + row["TB008"] + "',TB009='" + row["TB009"] + "',TB010='" + row["TB010"] + "',TB011='" + row["TB011"] + "',TB012='" + row["TB012"] + "',TB013='" + row["TB013"] + "',TB014='" + row["TB014"] + "',TB015='" + row["TB015"] + "',TB016='" + row["TB016"] + "',TB017='" + row["TB017"] + "',TB018='" + row["TB018"] + "',TB019='" + row["TB019"] + "',TB020='" + row["TB020"] + "',TB021='" + row["TB021"] + "',TB022='" + row["TB022"] + "',TB023='" + row["TB023"] + "',TB024='" + row["TB024"] + "',TB025='" + row["TB025"] + "',TB026='" + row["TB026"] + "',TB027='" + row["TB027"] + "',TB028='" + row["TB028"] + "',TB029='" + row["TB029"] + "',TB030='" + row["TB030"] + "',TB031='" + row["TB031"] + "' WHERE TB001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' AND TB002='" + row["TB002"] + "'";
                                DBLink.LKSCM(upSCMsql);
                            }
                            else
                            {
                                string insertSCMsql = "INSERT SCMTB(CreateDate,CreateBy,ModiDate,ModiBy,Flag,TB001,TB002,TB003,TB004,TB005,TB006,TB007,TB008,TB009,TB010,TB011,TB012,TB013,TB014,TB015,TB016,TB017,TB018,TB019,TB020,TB021,TB022,TB023,TB024,TB025,TB026,TB027,TB028,TB029,TB030,TB031) VALUES (SYSDATETIME(),'" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//User") + "',NULL,'',1,'" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "','" + row["SUP"] + "','" + row["TB003"] + "','" + row["TB004"] + "','" + row["TB005"] + "','" + row["TB006"] + "','" + row["TB007"] + "','" + row["TB008"] + "','" + row["TB009"] + "','" + row["TB010"] + "','" + row["TB011"] + "','" + row["TB012"] + "','" + row["TB013"] + "','" + row["TB014"] + "','" + row["TB015"] + "','" + row["TB016"] + "','" + row["TB017"] + "','" + row["TB018"] + "','" + row["TB019"] + "','" + row["TB020"] + "','" + row["TB021"] + "','" + row["TB022"] + "','" + row["TB023"] + "','" + row["TB024"] + "','" + row["TB025"] + "','" + row["TB026"] + "','" + row["TB027"] + "','" + row["TB028"] + "','" + row["TB029"] + "','" + row["TB030"] + "','" + row["TB031"] + "')";
                                DBLink.LKSCM(insertSCMsql);
                            }
                        }
                        //更新ERP
                        string upERPsql = "UPDATE " + XMLHelper.GetInnerTextByXpath("Configure.xml", "//POUpdateDB") + " SET " + XMLHelper.GetInnerTextByXpath("Configure.xml", "//POUpdateField") + "='3' WHERE TD001 + '-' + TD002 + '-' + TD003='" + row["TB003"] + "'";
                        DBLink.LKERP(upERPsql);
                        //更新供应商信息中的“采购订单更新时间”
                        string upSCMsql_time = "UPDATE SCMME SET ME009='" + row["ModiDate"] + "' WHERE ME001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' AND ME002='" + row["SUP"] + "'";
                        DBLink.LKSCM(upSCMsql_time);
                    }
                    //发mail
                    SendSUPMail(SUP, dt);
                }
            }
            else
            {
                
            }
            if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//POCYC") == "按天")
            {
                timer3.Interval = 24 * 60 * 60 * 1000;
            }
        }
        private void Do_action4(RichTextBox rtb)
        {
            //for (int i = 0; i < 10; i++)
            //{
            //    this.BeginInvoke(new ClientDelegate(delegate
            //    {
            //        rtb.AppendText(i.ToString());
            //    }));
            //    System.Threading.Thread.Sleep(100);
            //}
            if (PanduanDate(XMLHelper.GetInnerTextByXpath("Configure.xml", "//PRStrDate"), XMLHelper.GetInnerTextByXpath("Configure.xml", "//PREndDate"), timer4))
            {
                // 1）执行查询SQL。
                string sqlERP = XMLHelper.GetInnerTextByXpath("Configure.xml", "//PRLift");
                DataTable dt = DBLink.LinkERP(sqlERP, "ERPPR");
                if (dt.Rows.Count > 0)
                {
                    List<string> SUP = new List<string>();
                    dt = DBLink.TableaddSupCol_R(dt);
                    foreach (DataRow row in dt.Rows)
                    {
                        if (!SUP.Contains(row["SUP"]))
                        {
                            SUP.Add(row["SUP"].ToString());
                        }
                        if (row["ZT"].ToString() == "2")
                        {
                            //delete
                            string delSCMsql = "DELETE FROM SCMTE WHERE TE001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' AND TE002='" + row["TE002"] + "'";
                            DBLink.LKSCM(delSCMsql);
                        }
                        else
                        {
                            string sqlSCM = "select TE003 FROM SCMTE where TE001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "'AND  TE002='" + row["TE002"] + "'";
                            if (DBLink.LKSCM(sqlSCM) > 0)
                            {
                                string upSCMsql = "UPDATE SCMTE SET ModiDate='" + row["ModiDate"] + "',ModiBy='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//User") + "',Flag=Flag+1,TE003='" + row["TE003"] + "',TE004='" + row["TE004"] + "',TE005='" + row["TE005"] + "',TE006='" + row["TE006"] + "',TB007='" + row["TE007"] + "',TE008='" + row["TE008"] + "',TE009='" + row["TE009"] + "',TE010='" + row["TE010"] + "',TE011='" + row["TE011"] + "',TE012='" + row["TE012"] + "',TE013='" + row["TE013"] + "',TE014='" + row["TE014"] + "',TE015='" + row["TE015"] + "',TE016='" + row["TE016"] + "',TE017='" + row["TE017"] + "',TE018='" + row["TE018"] + "',TE019='" + row["TE019"] + "',TE020='" + row["TE020"] + "',TE021='" + row["TE021"] + "',TE022='" + row["TE022"] + "' WHERE TE001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' AND TE002='" + row["TE002"] + "'";
                                DBLink.LKSCM(upSCMsql);
                            }
                            else
                            {
                                string insertSCMsql = "INSERT SCMTE(CreateDate,CreateBy,ModiDate,ModiBy,Flag,TE001,TE002,TE003,TE004,TE005,TE006,TE007,TE008,TE009,TE010,TE011,TE012,TE013,TE014,TE015,TE016,TE017,TE018,TE019,TE020,TE021,TE022) VALUES (SYSDATETIME(),'" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//User") + "',NULL,'',1,'" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "','" + row["SUP"] + "','" + row["TE003"] + "','" + row["TE004"] + "','" + row["TE005"] + "','" + row["TE006"] + "','" + row["TE007"] + "','" + row["TE008"] + "','" + row["TE009"] + "','" + row["TE010"] + "','" + row["TE011"] + "','" + row["TE012"] + "','" + row["TE013"] + "','" + row["TE014"] + "','" + row["TE015"] + "','" + row["TE016"] + "','" + row["TE017"] + "','" + row["TE018"] + "','" + row["TE019"] + "','" + row["TE020"] + "','" + row["TE021"] + "','" + row["TE022"]+"')";
                                DBLink.LKSCM(insertSCMsql);
                            }
                        }
                        //更新ERP
                        string upERPsql = "UPDATE " + XMLHelper.GetInnerTextByXpath("Configure.xml", "//PRUpdateDB") + " SET " + XMLHelper.GetInnerTextByXpath("Configure.xml", "//PRUpdateField") + "='3' WHERE TJ001 + '-' + TJ002 + '-' + TJ003='" + row["TE002"] + "'";
                        DBLink.LKERP(upERPsql);
                        //更新供应商信息中的“采购退货单更新时间”
                        string upSCMsql_time = "UPDATE SCMME SET ME010='" + row["ModiDate"] + "' WHERE ME001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' AND ME002='" + row["SUP"] + "'";
                        DBLink.LKSCM(upSCMsql_time);
                    }
                    //发mail
                    SendSUPMail(SUP, dt);
                }
            }
            else
            {
                
            }
            if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//PRCYC") == "按天")
            {
                timer4.Interval = 24 * 60 * 60 * 1000;
            }
        }
        private void Do_action5(RichTextBox rtb)
        {
            if (PanduanDate(XMLHelper.GetInnerTextByXpath("Configure.xml", "//InvStrDate"), XMLHelper.GetInnerTextByXpath("Configure.xml", "//InvEndDate"), timer5))
            {
                string sqlERP = XMLHelper.GetInnerTextByXpath("Configure.xml", "//InvLift");
                DataTable dt = DBLink.LinkERP(sqlERP, "ERPInv");
                foreach (DataRow row in dt.Rows)
                {
                    string sqlSCM = "select TA002 FROM SCMTA where TA002='" + row["TA002"] + "' AND TA010='"+ row["TA010"] + "' AND TA001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "'";
                    if (DBLink.LKSCM(sqlSCM) > 0)
                    {
                            //Update
                            string upSCMsql = "UPDATE SCMTA SET ModiDate='"+ row["ModiDate"] + "',ModiBy='"+ XMLHelper.GetInnerTextByXpath("Configure.xml", "//User") + "',Flag=Flag+1,TA003='" + row["TA003"] + "',TA004='" + row["TA004"] + "',TA005='" + row["TA005"] + "',TA006='" + row["TA006"] + "',TA007='" + row["TA007"] + "',TA008='" + row["TA008"] + "',TA009='"+ row["TA009"] + "',TA011='"+ row["TA011"] + "',TA012='"+ row["TA012"] + "' WHERE TA001='" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' AND TA002='" + row["TA002"] + "' AND TA010='"+row["TA010"]+"'";
                            DBLink.LKSCM(upSCMsql);
                    }
                    else
                    {
                            //insert
                            string upSCMsql = "INSERT SCMTA(CreateDate,CreateBy,ModiDate,ModiBy,Flag,TA001,TA002,TA003,TA004,TA005,TA006,TA007,TA008,TA009,TA010,TA011,TA012) VALUES (SYSDATETIME(),'" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//User") + "',NULL,'',1,'" + XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "','" + row["TA002"] + "','" + row["TA003"] + "','" + row["TA004"] + "','" + row["TA005"] + "','" + row["TA006"] + "','" + row["TA007"] + "','" + row["TA008"] + "','"+ row["TA009"] + "','" + row["TA010"] + "','" + row["TA011"] + "','" + row["TA012"] + "')";
                            DBLink.LKSCM(upSCMsql);
                    }
                }
                //   3)更新供应商信息中的“库存明细更新时间”
                //string upERPsql = "UPDATE SCMME SET ME011=SYSDATETIME() WHERE ";
                //DBLink.LKSCM(upERPsql);
            }
            else
            {
                
            }
            if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//InvCYC") == "按天")
            {
                timer5.Interval = 24 * 60 * 60 * 1000;
            }
        }
        private void Do_action6(RichTextBox rtb)
        {
            if (PanduanDate(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ASNStrDate"), XMLHelper.GetInnerTextByXpath("Configure.xml", "//ASNEndDate"), timer6))
            {
                Int32 DOC_NO = 0;
                if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERPType") == "1.易飞ERP进货")
                {
                    string sqlSCM = "SELECT TC002 AS SHDH,TB031 AS JHDB,TD003 AS SHDXH,ME006 AS ERPGYS,TD004 AS CGDDDH,TB015 AS SZ,TB016 AS SL,TB017 AS BZ,TD005 AS BCCHSL,TB011 AS DW FROM SCMTC LEFT JOIN SCMTD ON TC001 = TD001 AND TC002 = TD002 LEFT JOIN SCMTB ON TB001 = TC003 AND TB003 = TD004 LEFT JOIN SCMME ON TB001 = ME001 AND TB002 = ME002 WHERE   TD015 = '0' AND TC001 = '"+ XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' ORDER BY TC002,TB031,ME006,TB015,TB016,TB017,TD004";
                    string sqlDistSCM = "SELECT TC002 AS SHDH,TB031 AS JHDB,ME006 AS ERPGYS,TB015 AS SZ,TB016 AS SL,TB017 AS BZ FROM SCMTC LEFT JOIN SCMTD ON TC001 = TD001 AND TC002 = TD002 LEFT JOIN SCMTB ON TB001 = TC003 AND TB003 = TD004 LEFT JOIN SCMME ON TB001 = ME001 AND TB002 = ME002 WHERE   TD015 = '0' AND TC001 = '"+ XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' GROUP BY TC002,TB031,ME006,TB015,TB016,TB017";
                    
                    DataTable dt = DBLink.LinkERP(sqlSCM, "ASN");
                    DataTable dtDist = DBLink.LinkERP(sqlDistSCM, "ASNDist");

                    dtDist.Columns.Add("DOC_NO");
                    dt.Columns.Add("DOC_NO");

                    if (dtDist.Rows.Count > 0)
                    {
                        string MaxDocNO = "SELECT ISNULL(MAX(TG002),CONVERT(varchar(100), GETDATE(), 112)+'000') FROM PURTG WHERE TG001='" + dtDist.Rows[0]["JHDB"] +"' AND SUBSTRING(TG002,1,4)=YEAR(GETDATE()) AND SUBSTRING(TG002,5,2)=MONTH(GETDATE()) AND  SUBSTRING(TG002,7,2)=DAY(GETDATE())";
                        DOC_NO = Int32.Parse(MaxDocNO)+1;
                        foreach (DataRow row in dtDist.Rows)
                        {
                            row["DOC_NO"] = DOC_NO;
                            string instERPsql = "INSERT INTO PURTG ( TG001 ,TG002 ,TG003 ,TG004 ,TG005 ,TG006 ,TG007 ,TG008 ,TG009 ,TG010 ,TG011 ,TG012 ,TG013 ,TG014 ,TG015 ,TG016 ,TG017 ,TG018 ,TG019 ,TG020 ,TG021 ,TG022 ,TG025 ,TG026 ,TG027 ,TG028 ,TG030 ,TG031 ,TG032 ,TG033 ,TG040 ,TG041 ,TG042 ,TG044 ,TG045 ,TG046 ,TG047 ,TG048 ,TG049 ,TG050 ,TG053 ,TG054 ,TG056 ,TG057 ,TG058 ,TG059 ,TG501) VALUES ('"+row["JHDB"]+ "' ,'"+ row["DOC_NO"] + "' ,SYSDATETIME() ,PURTC.TC010 ,PURTC.TC004 ,'' ,PURTC.TC005 ,PURTC.TC006 ,PURMA.MA030 ,PURTC.TC018 ,'' ,0 ,'N' ,SYSDATETIME() ,'N' ,'' ,0 ,0 , 0 ,0 ,PURMA.MA003 ,PURMA.MA005 ,0 ,0 ,'' ,0 ,PURTC.TC026 ,0 ,0 ,'' ,0 ,0 ,'N' ,'' , 0 ,0 ,'' ,'' ,'' ,'N' ,0 ,0 ,'' ,'' ,'0' ,'' ,'" + row["SHDH"]+"')";
                            DBLink.LKERP(instERPsql);
                            if (dt.Rows.Count > 0)
                            {
                                foreach (DataRow row_d in dt.Rows)
                                {
                                    if (row["SHDH"].Equals(row_d["SHDH"]) && row["JHDB"].Equals(row_d["JHDB"]) && row["ERPGYS"].Equals(row_d["ERPGYS"]) && row["SZ"].Equals(row_d["SZ"]) && row["SL"].Equals(row_d["SL"]) && row["BZ"].Equals(row_d["BZ"]))
                                    {
                                        String[] sp = row_d["CGDDDH"].ToString().Split('-');
                                        string instERP_Dsql = "INSERT INTO PURTH(TH001 ,TH002 ,TH003 ,TH004 ,TH005 ,TH006 ,TH007 ,TH008 ,TH009 ,TH010 ,TH011 ,TH012 ,TH013 ,TH014 ,TH015 ,TH016 ,TH017 ,TH018 ,TH019 ,TH020 ,TH021 ,TH022 ,TH023 ,TH024 ,TH025 ,TH026 ,TH027 ,TH028 ,TH029 ,TH030 ,TH031 ,TH032 ,TH033 ,TH034 ,TH035 ,TH036 ,TH037 ,TH038 ,TH039 ,TH040 ,TH041 ,TH042 ,TH043 ,TH044 ,TH045 ,TH046 ,TH047 ,TH048 ,TH049 ,TH050 ,TH051 ,TH052 ,TH053 ,TH054 ,TH055 ,TH056 ,TH057 ,TH058 ,TH059 ,TH061 ,TH062 ,TH064 ,TH065 ,TH066 ,TH067 ,TH068 ,TH069 ,TH070 ,TH071 ,TH072 ,TH073 ,TH080 ,THC02 ,THC03 ,THC04 ,THC05 ,THC06 ,THC07 ,TH501,TH502)(SELECT '" + row_d["JHDB"] + "' ,'"+ row["DOC_NO"] + "' ,'" + row_d["SHDXH"] + "' ,PURTD.TD004 ,PURTD.TD005 ,PURTD.TD006 ," + row_d["BCCHSL"] + " ,'" + row_d["DW"] + "' ,INVMB.MB017 ,'********************' ,PURTD.TD001  ,PURTD.TD002 , PURTD.TD003  , SYSDATETIME() ," + row_d["BCCHSL"] + " ,PURTD.TD039*(" + row_d["BCCHSL"] + "/PURTD.TD008) ,0 ,PURTD.TD010 ,PURTD.TD039*(" + row_d["BCCHSL"] + "/PURTD.TD008)*PURTD.TD010 ,0 ,'' ,'' ,'' ,0 ,'' ,'N' ,IIF(PUR.TD012<SYSDATETIME(),'Y','N') ,IIF(INV.MB043='0','0','1') ,'N' ,'N' ,'N' ,'N' ,PURTD.TD014 ,PURTD.TD019*(" + row_d["BCCHSL"] + "/PURTD.TD008) ,PURTD.TD020 ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'N' ,'N' ,PURTD.TD034*(" + row_d["BCCHSL"] + "/PURTD.TD008) ,PURTD.TD035*(" + row_d["BCCHSL"] + "/PURTD.TD008) ,PURTD.TD034*(" + row_d["BCCHSL"] + "/PURTD.TD008)*PURTC.TC006 ,PURTD.TD035*(" + row_d["BCCHSL"] + "/PURTD.TD008)*PURTC.TC006 ,PURTD.TD030*(" + row_d["BCCHSL"] + "/PURTD.TD008) ,PURTD.TD030*(" + row_d["BCCHSL"] + "/PURTD.TD008) ,0 ,0 ,PURTD.TD032 ,0 ,0 ,'' ,'' ,0 ,0 ,0 ,0 ,PURTD.TD038 ,INVMB.MB004 ,'' ,'' ,0 ,0 ,'N' ,1 ,'' ,'' ,'N' ,'2' ,'' ,'' ,'' ,'' ,'' ,'" + row_d["SHDH"] + "+' - '+" + row_d["SHDXH"] + "','1' FROM PURTD INNER JOIN PURTC ON PURTC.TC001=PURTD.TD001 AND PURTC.TC002=PURTD.TD002 INNER JOIN PURMA ON PURTC.TC004=PURMA.MA001 INNER JOIN INVMB ON PURTD.TD004=INVMB.MB001 WHERE PURTD.TD001='" + sp[0] + "' AND PURTD.TD002='" + sp[1] + "' AND PURTD.TD003='" + sp[2] + "' )";
                                        DBLink.LKERP(instERP_Dsql);
                                        string updSCMsta = "UPDATE SCMTD SET TD015='1',TD016=SYSDATETIME() WHERE TD002+TD003='"+row_d["SHDH"] +row_d["SHDXH"] +"'";
                                        DBLink.LKSCM(updSCMsta);
                                    }
                                }
                            }
                            DOC_NO++;
                        }
                        string SumDanTou = "UPDATE PURTG SET TG017=A.TH019,TG019=A.TH046,TG026=A.TH015,TG028=A.TH045,TG031=A.TH047,TG032=A.TH048,TG040=A.TH050,TG053=A.TH007,TG054=A.TH049 FROM (SELECT TH001,TH002,SUM(TH019)AS TH019,SUM(TH046) AS TH046,SUM(TH015) AS TH015,SUM(TH045)AS TH045,SUM(TH047)AS TH047,SUM(TH048)AS TH048,SUM(TH050)AS TH050,SUM(TH007)AS TH007,SUM(TH049)AS TH049 FROM PURTH WHERE TH001='" + dtDist.Rows[0]["JHDB"] + "' AND SUBSTRING(TH002,1,8)=CONVERT(varchar(100), GETDATE(), 112) AND TH002>='" + MaxDocNO + "' GROUP BY TH001,TH002) AS A WHERE TG001=A.TH001 AND TG002=A.TH002";
                        DBLink.LKERP(SumDanTou);
                    }
                    
                }
                else if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERPType") == "2.易飞ERP到货单")
                {
                    string sqlSCM = "SELECT  TC002 AS SHDH,TB031 AS DHDB,TD003 AS SHDXH,ME006 AS ERPGYS,TD004 AS CGDDDH,TD005 AS BCCHSL,TB011 AS DW FROM SCMTC LEFT JOIN SCMTD ON TC001 = TD001 AND TC002 = TD002 LEFT JOIN SCMTB ON TB001 = TC003 AND TB003 = TD004 LEFT JOIN SCMME ON TB001 = ME001 AND TB002 = ME002 WHERE   TD015 = '0' AND TC001 = '"+ XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' ORDER BY TC002,TB031,ME006,TD004";
                    string sqlDistSCM = "SELECT  TC002 AS SHDH,TB031 AS DHDB,ME006 AS ERPGYS FROM SCMTC LEFT JOIN SCMTD ON TC001 = TD001 AND TC002 = TD002 LEFT JOIN SCMTB ON TB001 = TC003 AND TB003 = TD004 LEFT JOIN SCMME ON TB001 = ME001 AND TB002 = ME002 WHERE   TD015 = '0' AND TC001 = '"+ XMLHelper.GetInnerTextByXpath("Configure.xml", "//CompanyCode") + "' GROUP BY TC002,TB031,ME006";

                    DataTable dt = DBLink.LinkERP(sqlSCM, "ASN");
                    DataTable dtDist = DBLink.LinkERP(sqlDistSCM, "ASNDist");

                    dtDist.Columns.Add("DOC_NO");
                    dt.Columns.Add("DOC_NO");

                    if (dtDist.Rows.Count > 0)
                    {
                        string MaxDocNO = "SELECT ISNULL(MAX(CC002),CONVERT(varchar(100), GETDATE(), 112)+'000') FROM PURCC WHERE CC001='" + dtDist.Rows[0]["DHDB"] + "' AND SUBSTRING(CC002,1,4)=YEAR(GETDATE()) AND SUBSTRING(CC002,5,2)=MONTH(GETDATE()) AND  SUBSTRING(CC002,7,2)=DAY(GETDATE())";
                        DOC_NO = Int32.Parse(MaxDocNO) + 1;
                        foreach (DataRow row in dtDist.Rows)
                        {
                            row["DOC_NO"] = DOC_NO;
                            string instERPsql = "INSERT INTO PURCC(CC001 ,CC002 ,CC003 ,CC004 ,CC005 ,CC006 ,CC007 ,CC008 ,CC009 ,CC010 , CC011 ,CC012 ,CC013 ,CC014 ,CC015 ,CC026 ,CC027,CC501)VALUES('" + row["DHDB"] + "' ,'"+ MaxDocNO + "' , SYSDATETIME() ,PURTC.TC010 ,PURTC.TC004 ,'' ,0 ,'1' ,'N' , SYSDATETIME() ,'' ,PURMA.MA003 ,0 ,0 ,'' ,'' ,'N' ,'" + row["SHDH"] + "' )";
                            DBLink.LKERP(instERPsql);
                            if (dt.Rows.Count > 0)
                            {
                                foreach (DataRow row_d in dt.Rows)
                                {
                                    if(row["SHDH"].Equals(row_d["SHDH"]) && row["DHDB"].Equals(row_d["DHDB"]) && row["ERPGYS"].Equals(row_d["ERPGYS"]))
                                    {
                                        string instERP_Dsql = "INSERT INTO PURCD(CD001 ,CD002 ,CD003 ,CD004 ,CD005 ,CD006 ,CD007 ,CD008 ,CD010 ,CD011 ,CD012 ,CD013 ,CD014 ,CD015 ,CD016 ,CD017 ,CD018 ,CD019 ,CD020 ,CD021 ,CD022 ,CD023 ,CD024 ,CD025 ,CD026 ,CD027 ,CD028 ,CD029 ,CD031 ,CD032 ,CD033 ,CD034 ,CD035 ,CD036 ,CD038 ,CD501,CD502)VALUES  ('" + row_d["DHDB"] + "' ,'"+ MaxDocNO + "' ,'" + row_d["SHDXH"] + "' , PURTD.TD004 ,PURTD.TD005 ,PURTD.TD006 ,'" + row_d["DW"] + "' ," + row_d["BCCHSL"] + ",PURTD.TD001  ,PURTD.TD002  ,PURTD.TD003  ,PURTD.TD008  ,PURTD.TDC01  ,PURTD.TD009  ,PURTD.TD007  ,PURTD.TD022  ,0 ,0 ,0 ,INVMB.MB004 ,PURTD.TD020  ,PURTD.TD030*(" + row_d["BCCHSL"] + "/PURTD.TD008) ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,PURTD.TD032 ,'N' ,'' ,'' ,'N' ,'','1')";
                                        DBLink.LKERP(instERP_Dsql);
                                        string updSCMsta = "UPDATE SCMTD SET TD015='1',TD016=SYSDATETIME() WHERE TD002+TD003='" + row_d["SHDH"] + row_d["SHDXH"] + "'";
                                        DBLink.LKSCM(updSCMsta);
                                    } 
                                }
                            }
                            DOC_NO++;
                        }
                        string SumDanTou = "UPDATE PURCC SET CC013=CD008,CC014=CD023 FROM (SELECT CD001,CD002,SUM(CD008)AS CD008,SUM(CD023)AS CD023 FROM PURCD WHERE CD001='"+ dtDist.Rows[0]["DHDB"] + "' AND SUBSTRING(CD002,1,8)=CONVERT(varchar(100), GETDATE(), 112) AND CD002>='"+ MaxDocNO + "' GROUP BY CD001,CD002) AS A WHERE CC001=A.CD001 AND CC002=A.CD002";
                        DBLink.LKERP(SumDanTou);
                    } 
                }
            }
            else
            {
                
            }
            if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//ASNCYC") == "按天")
            {
                timer6.Interval = 24 * 60 * 60 * 1000;
            }
        }
        private void Do_action7(RichTextBox rtb)
        {
            if (PanduanDate(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERP_ASNStrDate"), XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERP_ASNEndDate"), timer7))
            {
                if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERPType")== "1.易飞ERP进货")
                {
                    string sqlERP = "SELECT TH015,TH016,TH017,TH011,TH012,TH013,TH501 FROM PURTH WHERE TH030='Y' AND TH502='1' AND TH051<>'' ORDER BY TH014,TH051";
                    DataTable dt = DBLink.LinkERP(sqlERP, "ERP_ASN");
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            string upSCMsql = "UPDATE SCMTD SET TD006="+row["TH015"]+",TD007="+row["TH016"]+",TD008="+row["TH017"]+",TD009='2',TD011='"+row["TH011"]+"-"+row["TH012"]+"-"+row["TH013"]+"',TD012=SYSDATETIME() WHERE TD002+'-'+TD003='"+row["TH501"]+"'";
                            DBLink.LKSCM(upSCMsql);
                            string upERPsql = "UPDATE PURTH SET TH502='2' WHERE TH501='"+row["TH501"]+"'";
                            DBLink.LKERP(upERPsql);
                        }
                    }
                }
                else if(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERPType") == "2.易飞ERP到货单")
                {
                    string sqlERP = "SELECT CD008,CD018,CD019,CD010,CD011,CD012,CD051 FROM PURCD WHERE CD034='Y' AND CD502='1' AND CD051<>'' AND (CD018>0 OR CD019>0) ORDER BY CD051";
                    DataTable dt = DBLink.LinkERP(sqlERP, "ERP_ASN");
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            string upSCMsql = "UPDATE SCMTD SET TD006=" + row["CD018"] + ",TD007=" + row["CD019"] + ",TD008=0,TD009='"+(Int32.Parse(row["CD018"].ToString())+ Int32.Parse(row["CD019"].ToString())>= Int32.Parse(row["CD008"].ToString()) ? "1":"2")+"',TD011='" + row["CD010"] + "-" + row["CD011"] + "-" + row["CD012"] + "',TD012=SYSDATETIME() WHERE TD002+'-'+TD003='" + row["CD501"] + "'";
                            DBLink.LKSCM(upSCMsql);
                            string upERPsql = "UPDATE PURCD SET CD502='2' WHERE CD501='" + row["CD501"] + "'";
                            DBLink.LKERP(upERPsql);
                        }
                    }
                }
            }
            else
            {

            }
            if (XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERP_ASNCYC") == "按天")
            {
                timer7.Interval = 24 * 60 * 60 * 1000;
            }
            
        }
        private void button3_Click(object sender, EventArgs e)
        {
            button3.Enabled = false;
            timer1.Enabled = false;
            timer2.Enabled = false;
            timer3.Enabled = false;
            timer4.Enabled = false;
            timer5.Enabled = false;
            timer6.Enabled = false;
            timer7.Enabled = false;
            if (checkBox1.Checked)
            {
                t1.Abort();
            }
            if (checkBox2.Checked)
            {
                t2.Abort();
            }
            if (checkBox3.Checked)
            {
                t3.Abort();
            }
            if (checkBox4.Checked)
            {
                t4.Abort();
            }
            if (checkBox5.Checked)
            {
                t5.Abort();
            }
            if (checkBox6.Checked)
            {
                t6.Abort();
            }
            if (checkBox7.Checked)
            {
                t7.Abort();
            }
            button2.Enabled = true;
        }

        private void Main_Run_Load(object sender, EventArgs e)
        {
            checkBox1.Checked = Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//Chkemail"));
            checkBox2.Checked = Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ItemCheck"));
            checkBox3.Checked = Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//POCheck"));
            checkBox4.Checked = Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//PRCheck"));
            checkBox5.Checked = Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//InvCheck"));
            checkBox6.Checked = Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ASNCheck"));
            checkBox7.Checked = Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//ERP_ASNCheck"));
        }

        private DataTable getTable(string strCon, string strSql, string strTable)
        {
            try
            {
                SqlConnection sqlcon = new SqlConnection(strCon);
                SqlDataAdapter da = new SqlDataAdapter(strSql, sqlcon);
                DataTable dt = new DataTable(strTable);
                da.Fill(dt);
                sqlcon.Close();
                sqlcon.Dispose();
                return dt;
            }
            catch
            {
                return null;
            }
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
                    t1 = new Thread(
            new ThreadStart(new ClientDelegate(delegate
            {
                this.Do_action1(this.richTextBox1);
            })));
                        t1.Start();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
                    t2 = new Thread(
            new ThreadStart(new ClientDelegate(delegate
            {
                this.Do_action2(this.richTextBox2);
            })));
                    t2.Start();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
                    t3 = new Thread(
               new ThreadStart(new ClientDelegate(delegate
               {
                   this.Do_action3(this.richTextBox2);
               })));
                    t3.Start();
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
                    t4 = new Thread(
              new ThreadStart(new ClientDelegate(delegate
              {
                  this.Do_action4(this.richTextBox1);
              })));
                    t4.Start();
        }

        private void timer5_Tick(object sender, EventArgs e)
        {
                    t5 = new Thread(
              new ThreadStart(new ClientDelegate(delegate
              {
                  this.Do_action5(this.richTextBox2);
              })));
                    t5.Start();
        }

        private void timer6_Tick(object sender, EventArgs e)
        {
                    t6 = new Thread(
             new ThreadStart(new ClientDelegate(delegate
             {
                 this.Do_action6(this.richTextBox2);
             })));
                    t6.Start();
        }

        private void timer7_Tick(object sender, EventArgs e)
        {
                    t7 = new Thread(
             new ThreadStart(new ClientDelegate(delegate
             {
                 this.Do_action7(this.richTextBox2);
             })));
                    t7.Start();
        }

        public string GetHtmlString(string ExportFileName, bool isPrint, DataTable tbl, DataTable tbl1)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<HTML><HEAD>");
            sb.Append("<title>" + ExportFileName + "</title>");
            sb.Append("<META HTTP-EQUIV='content-type' CONTENT='text/html; charset=GB2312'> ");
            sb.Append("<script language=javascript>");
            sb.Append("self.resizeBy(0,0);");
            sb.Append("self.resizeTo(screen.availWidth,screen.availHeight);");
            sb.Append("</script>");
            sb.Append("<style type=text/css>");
            sb.Append("td{font-size: 9pt;border:solid 1 #000000;}");
            sb.Append("table{padding:3 0 3 0;border:solid 1 #000000;margin:0 0 0 0;BORDER-COLLAPSE: collapse;}");
            sb.Append("</style>");
            sb.Append("</HEAD>");
            if (!isPrint)
                sb.Append("<BODY  >");
            else
                sb.Append("<BODY   onload = 'window.print()'>");
            sb.Append("尊敬的 ***：<br> 您好！<br> OpenSCM供应链管理平台(www.OpenSCM.net)智能接口工具对贵司基础资料<span style='color:red;'>异常检测结果如下</span>，还请及时进行调整，谢谢！<br><br> <span style='color:red;'>供应商检测结果:</span><br><br>");
            //sb.Append("");
            sb.Append("<table cellSpacing='0' cellPadding='0' width ='100%' border='1'");
            sb.Append(">");
            sb.Append("<tr valign='middle' bgcolor=aqua>");
            //sb.Append("<td><b>" + CommonUI.Translate("RowSequences") + "</b></td>");
            foreach (DataColumn column in tbl.Columns)
            {
                sb.Append("<td><b><span>" + column.ColumnName + "</span></b></td>");
            }
            sb.Append("</tr>");
            int iColsCount = tbl.Columns.Count;
            int rowsCount = tbl.Rows.Count - 1;
            for (int j = 0; j <= rowsCount; j++)
            {
                //sb.Append("<tr>");
                //sb.Append("<td>" + ((int)(j + 1)).ToString() + "</td>");
                for (int k = 0; k <= iColsCount - 1; k++)
                {
                    sb.Append("<td");
                    sb.Append(">");
                    object obj = tbl.Rows[j][k];
                    if (obj == DBNull.Value)
                    {
                        // 如果是NULL则在HTML里面使用一个空格替换之  
                        obj = "&nbsp;";
                    }
                    if (obj.ToString() == "")
                    {
                        obj = "&nbsp;";
                    }
                    string strCellContent = obj.ToString().Trim();
                    sb.Append("<span>" + strCellContent + "</span>");
                    sb.Append("</td>");
                }
                sb.Append("</tr>");
            }
            sb.Append("</TABLE><br><br><br>");
            if (Convert.ToBoolean(XMLHelper.GetInnerTextByXpath("Configure.xml", "//Chkcustomer")))
            {
                sb.Append("<span style='color:red;'>客户检测结果:</span><br><br>");
                sb.Append("<table cellSpacing='0' cellPadding='0' width ='100%' border='1'");
                sb.Append(">");
                sb.Append("<tr valign='middle' bgcolor=aqua>");
                //sb.Append("<td><b>" + CommonUI.Translate("RowSequences") + "</b></td>");
                foreach (DataColumn column in tbl1.Columns)
                {
                    sb.Append("<td><b><span>" + column.ColumnName + "</span></b></td>");
                }
                sb.Append("</tr>");
                iColsCount = tbl1.Columns.Count;
                rowsCount = tbl1.Rows.Count - 1;
                for (int j = 0; j <= rowsCount; j++)
                {
                    //sb.Append("<tr>");
                    //sb.Append("<td>" + ((int)(j + 1)).ToString() + "</td>");
                    for (int k = 0; k <= iColsCount - 1; k++)
                    {
                        sb.Append("<td");
                        sb.Append(">");
                        object obj = tbl1.Rows[j][k];
                        if (obj == DBNull.Value)
                        {
                            // 如果是NULL则在HTML里面使用一个空格替换之  
                            obj = "&nbsp;";
                        }
                        if (obj.ToString() == "")
                        {
                            obj = "&nbsp;";
                        }
                        string strCellContent = obj.ToString().Trim();
                        sb.Append("<span>" + strCellContent + "</span>");
                        sb.Append("</td>");
                    }
                    sb.Append("</tr>");
                }
                sb.Append("</TABLE>");
            }
            sb.Append("</BODY></HTML>");
            return sb.ToString();
        }

        private int CalcTime(string hour,string min)
        {
            int _hour = Convert.ToInt32(hour);
            int _min = Convert.ToInt32(min);
            DateTime dt1 = DateTime.Now;
            DateTime dt2 = DateTime.Today.AddHours(_hour).AddMinutes(_min);

            if (dt1.Hour > _hour || dt1.Hour == _hour && dt1.Minute >= _min)
            {
                dt2 = dt2.AddDays(1);
            }

            return Convert.ToInt32((dt2 - dt1).TotalMilliseconds);
        }

        private int CalcTime1(string min)
        {
            int _min = Convert.ToInt32(min);

            return _min*60*1000;
        }

        private bool PanduanDate(string startdate,string enddate,System.Windows.Forms.Timer t)
        {
            bool bol=false;
            DateTime dt = DateTime.Now;
            DateTime _startdate = DateTime.Parse(startdate).Date;
            DateTime _enddate = DateTime.Parse(enddate).Date.AddDays(1);
            if (dt>=_startdate && dt<=_enddate)
            {
                bol = true;
            }
            else if (dt > _enddate)
            {
                t.Enabled = false;
                bol = false;
            }
            else
            {
               
            }
            return bol;
        }

        private void SendSUPMail(List<string> lt,DataTable dt)
        {
            //foreach(string str in lt)
            //{

            //}
        }
    }
}
